package p.sunmes.les.action;

import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

public class EffectMoveToAction extends TemporalAction {
	private float startX, startY;
	private float endX, endY;
	private Drawable pic;

	public EffectMoveToAction(Drawable effectPic, float endX, float endY,
			float dt) {
		pic = effectPic;
		this.endX = endX;
		this.endY = endY;
		setDuration(dt);
		setInterpolation(Interpolation.exp5);
	}

	@Override
	protected void begin() {
		this.startX = target.getX();
		this.startY = target.getY();
	}

	@Override
	protected void update(float percent) {

		Group parent = target.getParent();
		Image tmpImg = new Image(pic);
		parent.addActor(tmpImg);
		tmpImg.setScale(target.getScaleX());
		tmpImg.setOrigin(target.getOriginX(), target.getOriginY());
		tmpImg.setPosition(target.getX(), target.getY());
		tmpImg.addAction(
				Actions.sequence(Actions.fadeOut(0.4f), Actions.removeActor()));

		target.setPosition(startX + (endX - startX) * percent,
				startY + (endY - startY) * Math.min(percent * percent, 1f));
	}

}
